/*******************************************************************************
 * Copyright (c) 2008 CEA LIST.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA LIST - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.activity.tabbedproperties.section;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ValueSpecification;
import org.eclipse.uml2.uml.ValueSpecificationAction;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.diagram.classdiagram.tabbedproperties.section.ValueSpecificationSection;

/**
 * Section for ValueSepcificationAction. 
 * <P>
 * There, user can define the value specification associated to this action
 * </P>
 */
public class ValueSpecificationActionSection extends PropertyViewSection {

	/** Section displayed */
	final protected ValueSpecificationSection valueSpecificationSection;

	/**
	 * Creates a new ValueSpecificationActionSection.
	 */
	public ValueSpecificationActionSection() {
		valueSpecificationSection = new ValueSpecificationSection() {
			@Override
			protected void addNewSpecification(Element container, ValueSpecification newSpecif) {
				super.addNewSpecification(container, newSpecif);
				if(container instanceof ValueSpecificationAction) {
					((ValueSpecificationAction)container).setValue(newSpecif);
				}
			}
		};
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		valueSpecificationSection.createControls(parent, tabbedPropertySheetPage);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void dispose() {
		valueSpecificationSection.dispose();
		super.dispose();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void refresh() {
		super.refresh();
		valueSpecificationSection.setValueSpecification(getValueSpecification());
		valueSpecificationSection.refresh();
	}
	
	/**
	 * Retrieve the value associated to the selected UML element, or 
	 * <code>null</code> if no value specification is given for the element. 
	 * @return the ValueSpecification that represents the value or 
	 * <code>null</code> if no value specification exists
	 */
	private ValueSpecification getValueSpecification() {
		if(umlElement instanceof ValueSpecificationAction) {
			return ((ValueSpecificationAction)umlElement).getValue();
		} 
		return null;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		valueSpecificationSection.setInput(part, selection);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void aboutToBeShown() {
		super.aboutToBeShown();
		valueSpecificationSection.aboutToBeShown();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void aboutToBeHidden() {
		super.aboutToBeHidden();
		valueSpecificationSection.aboutToBeHidden();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void selectionChanged(ISelection selection) {
		super.selectionChanged(selection);
		valueSpecificationSection.selectionChanged(selection);
	}
}
